package com.kevin.zhangchao.androidtest;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Selection;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by ZhangChao on 2016/12/11.
 */

public class PayPwdEditText extends RelativeLayout {

    private Context context;
    private EditText editText;
    private int pwdLength=6;
    private LinearLayout linearLayout;
    private TextView[] textViews;
    private OnTextFinishListener onTextFinishListener;

    public PayPwdEditText(Context context) {
        super(context);
        init(context);
    }

    public PayPwdEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public PayPwdEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    private void init(Context context) {
        this.context=context;
    }


    /**
     * @param bgdrawable 背景drawable
     * @param pwdlength 密码长度
     * @param splilinewidth 分割线宽度
     * @param splilinecolor 分割线颜色
     * @param pwdcolor 密码字体颜色
     * @param pwdsize 密码字体大小
     */
    public void initStyle(int bgdrawable, int pwdlength, float splilinewidth, int splilinecolor, int pwdcolor, int pwdsize)
    {
        this.pwdLength = pwdlength;
        initEdit(bgdrawable);
        initShowInput(bgdrawable, pwdlength, splilinewidth, splilinecolor, pwdcolor, pwdsize);
    }


    private void initEdit(int bgColor){
        editText=new EditText(context);
        editText.setBackgroundResource(bgColor);
        editText.setCursorVisible(false);
        editText.setTextSize(0);
        editText.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD|
        InputType.TYPE_CLASS_NUMBER);
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(pwdLength)});
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Editable editable=editText.getText();
                Selection.setSelection(editable,editable.length());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                initDatas(s);
                if (s.length()==pwdLength){
                    if (onTextFinishListener!=null){
                        onTextFinishListener.onFinish(s.toString().trim());
                    }
                }
            }


        });

        LayoutParams lp=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
        addView(editText,lp);
    }

    private void initDatas(Editable s) {
        if (s.length()>0){
            int length=s.length();
            for (int i=0;i<pwdLength;i++) {
                if (i < length) {
                    for (int j = 0; j < length; j++) {
                        char ch = s.charAt(j);
                        textViews[j].setText(String.valueOf(ch));
                    }
                } else{
                    textViews[i].setText("");
                }
            }
        }else{
            for (int i=0;i<pwdLength;i++){
                textViews[i].setText("");
            }
        }

    }

    public void initShowInput(int bgColor,int pwdLength,float slpilinewidth,int slplinecolor,
                              int pwdcolor,int pwdsize){
        linearLayout=new LinearLayout(context);
        linearLayout.setBackgroundResource(bgColor);
        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        addView(linearLayout);
        //add scret frame
        textViews=new TextView[pwdLength];
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        params.weight=1;
        params.gravity= Gravity.CENTER;

        LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams(dip2px(context,slpilinewidth),
                ViewGroup.LayoutParams.MATCH_PARENT);
        for (int i=0;i<textViews.length;i++){
            final  int index=i;
            TextView textView=new TextView(context);
            textView.setGravity(Gravity.CENTER);
            textViews[i]=textView;
            textViews[i].setTextSize(pwdsize);
            textViews[i].setTextColor(context.getResources().getColor(pwdcolor));
            textViews[i].setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD|
            InputType.TYPE_CLASS_NUMBER);
            linearLayout.addView(textView,params);

            if (i<textViews.length-1){
                View view=new View(context);
                view.setBackgroundColor(context.getResources().getColor(slplinecolor));
                linearLayout.addView(view,params2);
            }
        }
    }

    /**
     * 是否显示明文
     * @param showPwd
     */
    public void setShowPwd(boolean showPwd) {
        int length = textViews.length;
        for(int i = 0; i < length; i++) {
            if (showPwd) {
                textViews[i].setTransformationMethod(PasswordTransformationMethod.getInstance());
            } else {
                textViews[i].setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
        }
    }

    /**
     * 设置显示类型
     * @param type
     */
    public void setInputType(int type)
    {
        int length = textViews.length;
        for(int i = 0; i < length; i++) {
            textViews[i].setInputType(type);
        }
    }

    /**
     * 清除文本框
     */
    public void clearText()
    {
        editText.setText("");
        for(int i = 0; i < pwdLength; i++)
        {
            textViews[i].setText("");
        }
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public interface OnTextFinishListener
    {
        void onFinish(String str);
    }
}
